//
//  BaseModel+Swizzing.m
//  TestDemo
//
//  Created by fengxijun on 3/3/16.
//  Copyright © 2016 goappbox. All rights reserved.
//

#import "BaseModel+Swizzing.h"
#import <objc/runtime.h>

@implementation BaseModel (Swizzing)

+(void)load
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [self exchange];
    });
}

+ (void)exchange {
    SEL originalSelector = @selector(exchangeMethod);
    SEL swizzledSelector = @selector(ex_exchangeMethod);
    
    Method originMethod = class_getInstanceMethod([self class], originalSelector);
    Method swizzedMethod = class_getInstanceMethod([self class], swizzledSelector);
    
    
    BOOL didAddMethod =
    class_addMethod([self class],
                    originalSelector,
                    method_getImplementation(swizzedMethod),
                    method_getTypeEncoding(swizzedMethod));
    
    if (didAddMethod) {
        class_replaceMethod([self class],
                            swizzledSelector,
                            method_getImplementation(originMethod),
                            method_getTypeEncoding(originMethod));
    } else {
        method_exchangeImplementations(originMethod, swizzedMethod);
    }
}

-(void)ex_exchangeMethod
{
    NSLog(@"%s",__PRETTY_FUNCTION__);
    [self ex_exchangeMethod];
    //    [self ex_exchangeMethod];
}

@end
